package com.mvc4g.example.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.mvc4g.client.Controller;
import com.mvc4g.client.Event;
import com.mvc4g.client.ViewInterface;

public class SimpleCalculatorView extends Composite implements ViewInterface {
	
	private Controller controller = null;
	private TextBox screen = new TextBox();
	
	public SimpleCalculatorView(){
		
		screen.setWidth("50px"); screen.setEnabled(false); screen.setText("0");
		
		Button add = new Button("+1");
		add.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				//Create and fire event to the controller
				Event e = new Event("doOperation", "+");
				controller.handleEvent(e);				
			}			
		});
		
		Button less = new Button("-1");
		less.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				//Create and fire event to the controller
				Event e = new Event("doOperation", "-");
				controller.handleEvent(e);				
			}			
		});
		
		HorizontalPanel buttons = new HorizontalPanel();
		buttons.add(add); buttons.add(less);
		
		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.add(screen);mainPanel.add(buttons);
		
		RootPanel.get().add(mainPanel);
		
	}
	
	public void updateScreen(String value){
		screen.setText(value);
	}

	@Override
	public void setController(Controller controller) {
		this.controller = controller;
	}
	
}
